######################### HUMAN CODED DATA ANALYSIS ############################
################################################################################
#Name of code file: human_coded_analysis.R
#Purpose: Redo regression analyisis using only human coded data from Figure8
#Data In: human_coded_data.csv
#Data Out: Table B3 
#################################################################################

#Load Packages
library(readr)
library(apsrtable)

#Set Working Directory
setwd("~/Dropbox/egypt_tolerance_wp_replication/")

#Read in Data
data<-read_csv("data/human_coded_data.csv")
data[data=="#DIV/0!"]<-NA

#Tolerance Variables 
data$intolerant_total<-data$`Code 4` + data$`Code 5` + data$`Code 6`
data$intolerant_total[is.na(data$intolerant_total)]<-0
data$tolerant_total<-data$`Code 1` + data$`Code 2` + data$`Code 3`
data$relevant_total<-data$tolerant_total + data$intolerant_total
data$prop_intolerant<-data$intolerant_total/data$relevant_total
data$prop_intolerant[is.na(data$prop_intolerant)]<-0

#NETWORK VARS

#Elite Network Diversity Variables 
data$elite_diversity<- 1- abs(as.numeric(data$proportion_islamist_elites) - as.numeric(data$proportion_secular_elites))
#Deal with NAs 
data$elite_diversity <- replace(data$elite_diversity,which(is.na(data$elite_diversity)),1)

#Nonelite Network Diversity Variables
data$prop_islamist_nonelite<-data$non_elite_islamist_60/data$`non-elite_friends`
data$prop_secular_nonelite<-data$non_elite_secular_60/data$`non-elite_friends`
data$nonelite_diversity<- 1- abs(data$prop_islamist_nonelite - data$prop_secular_nonelite)
#Deal with NAs
data$nonelite_diversity <- replace(data$nonelite_diversity,which(is.na(data$nonelite_diversity)),1)

#Time vars 
data$date<-as.Date(data$`Date of First Tweet`, format="%m/%d/%y")
data$date_now<-as.Date('2016/11/01')
data$twitter_time<- difftime(data$date_now ,data$date , units = c("days"))
data$twitter_time<-as.numeric(data$twitter_time)

#Log vars
data$log_nonelite<-log(as.numeric(data$`non-elite_friends`+1))
data$log_elite<-log(data$total_elite_friends+1)
data$log_time<-log(data$twitter_time+1)
data$log_relevant<-log(data$relevant_total+1)

##########
#TABLE B3# 
##########

#OLS 
prop_intolerant_model <- lm(data$prop_intolerant ~ data$elite_diversity + data$nonelite_diversity + data$log_nonelite +data$log_elite  +data$Islamist + data$log_time)
summary(prop_intolerant_model) # show results
#QP Model
quasipoisson_intolerant_model <- glm(data$intolerant_total ~ data$elite_diversity + data$nonelite_diversity + data$log_nonelite +data$log_elite+ data$Islamist + data$log_relevant + data$log_time, family=quasipoisson())
summary(quasipoisson_intolerant_model)
(exp(coef(quasipoisson_intolerant_model)))

#Make Table
apsrtable(prop_intolerant_model, quasipoisson_intolerant_model, lev=.05, digits=3)



